<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-3-9 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'servlet/module/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
		
		$this->excute();
	}
	
	private function excute()
	{
		switch($this->act)
		{
			case 'login':
				$this->login();
				break;
			case 'do_register':
				$this->doRegister();
				break;
			case 'recommend_record':
				$this->recommendRecord();
				break;
			case 'feedback':
				$this->doFeedback();
				break;
			default:
				$this->message('g_04', STATUS_E);
				break;
		}
		
		$this->showResponse();
	}
	
	private function login()
	{
		//DBFunction::getInstance()->setDebug();
		
		if(!isset($this->request['username']) || !$this->request['username'])
		{
			$this->message('cu_01', STATUS_E);
		}
		if(!isset($this->request['password']) || !$this->request['password'])
		{
			$this->message('cu_02', STATUS_E);
		}
		
		$result = array();
		
		$request['phone'] = $this->request['username'];
		$user = get_visitor($request);
		if(isset($user['id']))
		{
			$password = $this->createPassword($user['create_date'], $this->request['password']);
			
			if($password == $user['password'])
			{
				$result['id'] = $user['id'];
				$result['phone'] = $user['phone'];
				$result['nickname'] = $user['nick_name'];
				$result['create_date'] = date('Y-m-d', $user['create_date']);
			}
		}
		
		if(!isset($result['id'])) $this->message('g_10', STATUS_F);
		
		$this->response['status'] = STATUS_S;
		$this->response['info'] = $result;
	}
	
	private function doRegister()
	{
		//DBFunction::getInstance()->setDebug();
		
		if(!isset($this->request['username']) || !$this->request['username'])
		{
			$this->message('cu_01', STATUS_E);
		}
		if(!isset($this->request['password']) || !$this->request['password'])
		{
			$this->message('cu_02', STATUS_E);
		}
		
		$time = time();
		
		$request['phone'] = $this->request['username'];
		$request['nick_name'] = isset($this->request['nickname']) ? $this->request['nickname'] : '';
		$request['last_ip'] = get_client_ip();
		$request['time'] = $time;
		
		$user = get_visitor($request);
		if(isset($user['id']))
		{
			$this->message('g_09', STATUS_F);
		}
		else
		{
			$request['password'] = $this->createPassword($time, $this->request['password']);
			
			$result = add_visitor($request);
			
			if(!$result)
			{
				$this->message('cu_03', STATUS_E);
			}
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['id'] = $result;
	}
	
	private function recommendRecord()
	{
		if(!isset($this->request['id']) || !$this->request['id'])
		{
			$this->message('g_05', STATUS_E);
		}
		
		$result = add_customer_recommend_record($this->request);
		
		if(!$result)
		{
			$this->message('g_07', STATUS_E);
		}
		
		$this->message('', STATUS_S);
	}
	
	private function doFeedback()
	{
		$this->request['feedback'] = isset($this->request['feedback']) ? $this->request['feedback'] : '';
		$this->request['name'] = isset($this->request['name']) ? $this->request['name'] : '';
		$this->request['telephone'] = isset($this->request['telephone']) ? $this->request['telephone'] : '';
		$this->request['email'] = isset($this->request['email']) ? $this->request['email'] : '';
		
		$result = add_feedback($this->request);
		
		if($result)
		{
			$this->message('g_06');
		}
		else
		{
			$this->message('g_07', STATUS_F);
		}
	}
}
